package com.cxhc.governance;

import com.cxhc.common.annotation.Anonymous;
import com.cxhc.common.core.domain.R;
import com.cxhc.governance.vo.PostExchangeVo;
import com.cxhc.system.domain.cockpit.CmInfo;
import com.cxhc.system.service.cockpit.ICmInfoService;
import com.cxhc.system.service.governance.IGovernanceService;
import lombok.SneakyThrows;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;


/**
 * 群治分
 */
@RestController()
@RequestMapping("/api/governance")
@CrossOrigin(origins = "*", maxAge = 3600)
public class ApiGovernance {

    @Resource
    private IGovernanceService governanceService;

    /**
     * 验证是否为环创用户
     */
    @Anonymous
    @GetMapping("/verificationUser/{idNumber}")
    public R<Boolean> verificationUser(@PathVariable String idNumber) {
        return R.ok(governanceService.verificationUser(idNumber));
    }

    /**
     * 查询用户积分余额
     */
    @Anonymous
    @GetMapping("/balance/{idNumber}")
    public R<Integer> balance(@PathVariable String idNumber) {
        return R.ok(governanceService.balance(idNumber));
    }

    /**
     * 积分余额兑换
     */
    @Anonymous
    @PostMapping("/exchange")
    public R<Boolean> exchange(@RequestBody @Valid PostExchangeVo vo) {
        // 判断用户
        Boolean verificationUser = governanceService.verificationUser(vo.getIdNumber());
        if(!verificationUser){
            return R.fail("用户不存在或该用户不是环创用户");
        }
        int balance = governanceService.balance(vo.getIdNumber());
        if(balance < vo.getScore()){
            return R.fail("积分不足");
        }
        governanceService.exchange(vo.getScore(), vo.getIdNumber());
        return R.ok();
    }


}
